/*********************************************************
          File Name:9.44.cpp
          Author: Abby Cin
          Mail: abbytsing@gmail.com
          Created Time: Tue 15 Sep 2015 10:20:46 AM CST
**********************************************************/

#include <iostream>
#include <string>

using namespace std;

void subxx(string &s, const string &oldVal, const string &newVal)
{
        for(string::size_type it = 0; it <= s.size() - oldVal.size(); )
        {
                string::size_type i = 0;
                while(s[it] == oldVal[i])
                {
                        ++it;
                        ++i;
                }
                it -= i;
                if(i >= oldVal.length())
                {
                        s.replace(it, oldVal.size(), newVal);
                        it += newVal.size();
                }
                else
                        ++it;
        }
}

int main()
{
        string s("tho tho tho, thru thru thru");

        cout << s << endl;

        subxx(s, "tho", "though");
        subxx(s, "thru", "through");

        cout << s << endl;
        return 0;
}
